#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django import template
from django.template.base import Node

register = template.Library()

@register.inclusion_tag("documents/item/item_add_inline.html")
def item_add_inline(document):
    return {'document': document}

@register.inclusion_tag("documents/item/item_add_document_inline.html")
def item_add_document_inline(module_part, module_term):
    return {'module_part': module_part, 'module_term': module_term}

@register.inclusion_tag("documents/item/item_change_inline.html")
def item_change_inline(item):
    return {'item': item}

@register.inclusion_tag("documents/item/item_delete_inline.html")
def item_delete_inline(item):
    return {'item': item}

@register.inclusion_tag("documents/item/item_fields.html", takes_context = True)
def item_fields(context, form, module_part, module_term):
    return {'form': form, 'module_part': module_part, 'module_term': module_term, 'request':context['request']}

@register.inclusion_tag("documents/item/document_fields.html", takes_context = True)
def document_fields(context, form, module_part, module_term):
    return {'form': form, 'module_part': module_part, 'module_term': module_term, 'request':context['request'] }

@register.inclusion_tag("documents/item/document_description.html")
def document_description():
    return {}

@register.inclusion_tag("documents/item/item_description.html")
def item_description():
    return {}

@register.tag
def add_item(parser, token):
    try:
        _, item_type_variable, document_variable = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires exactly two arguments" % token.contents.split()[0])
    return AddItemNode(item_type_variable, document_variable)
    
class AddItemNode(Node):
    """
    This Node represents the add_item_type tag
    It calls the plugin's renderer for this tag
    """
    def __init__(self, item_type_variable, document_variable):
        self.item_type_variable = template.Variable(item_type_variable)
        self.document_variable = template.Variable(document_variable)
        
    def render(self, context):
        try:
            # resolve the necessary variables from context
            item_type = self.item_type_variable.resolve(context)
            document = self.document_variable.resolve(context)
            
            # let the plugin worry about rendering
            return item_type.render_add_item(context, document)
        except template.VariableDoesNotExist:
            return ''
        
@register.tag
def add_document_item(parser, token):
    try:
        _, item_type_variable, module_part_variable, module_term_variable = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires exactly three arguments" % token.contents.split()[0])
    return AddDocumentItemNode(item_type_variable, module_part_variable, module_term_variable)
    
class AddDocumentItemNode(Node):
    """
    This Node represents the add_document_item_type tag
    It calls the plugin's renderer for this tag
    """
    def __init__(self, item_type_variable, module_part_variable, module_term_variable):
        self.item_type_variable = template.Variable(item_type_variable)
        self.module_part_variable = template.Variable(module_part_variable)
        self.module_term_variable = template.Variable(module_term_variable)
        
    def render(self, context):
        try:
            # resolve the necessary variables from context
            item_type = self.item_type_variable.resolve(context)
            module_part = self.module_part_variable.resolve(context)
            module_term = self.module_term_variable.resolve(context)
            
            # let the plugin worry about rendering
            return item_type.render_add_document_item(context, module_part, module_term)
        except template.VariableDoesNotExist:
            return ''
        
@register.tag
def show_item(parser, token):
    try:
        _, item_variable = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError("%r tag requires exactly three arguments" % token.contents.split()[0])
    return ShowItemNode(item_variable)
    
class ShowItemNode(Node):
    """
    This Node represents the show_item tag
    It calls the plugin's renderer for this tag
    """
    def __init__(self, item_variable):
        self.item_variable = template.Variable(item_variable)
        
    def render(self, context):
        try:
            # resolve the necessary variables from context
            item = self.item_variable.resolve(context)
            
            context['item'] = item
            
            # let the plugin worry about rendering
            return item.item_type.render_show_item(context, item)
        except template.VariableDoesNotExist:
            return ''